package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Epidemic;
import com.pojo.EpidemicSum;
import com.pojo.Result;
import com.pojo.Users;
import com.service.EpidemicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/epidemic")
@CrossOrigin
public class EpidemicController {
    @Autowired
    private EpidemicService epidemicService;
    //添加预约核酸申请记录
    @PostMapping("/add")
    public Result add(@RequestBody List<Epidemic> epidemics){
        System.out.println("add方法运行了");
        int count=0;
        Result result = new Result();
        for (Epidemic epidemic : epidemics) {
            System.out.println(epidemic);
            boolean flag = epidemicService.add(epidemic);
            if (!flag){
                count++;
            }
        }
        if(count==0){
            result.setMsg("预约申请成功，请在今晚10点后查看申请结果");
            result.setCode(200);
        }else{
            result.setMsg("预约申请失败，请检查申请信息是否填写完整");
            result.setCode(500);
        }

        return result;
    }



    //查询核酸预约记录
    @GetMapping("/{currentPage}/{pageSize}/{username}")
    public Result findByPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize,@PathVariable String username){
        System.out.println("根据页数查询方法运行了");
        List<Epidemic> list = epidemicService.findByPage(currentPage,pageSize,username);
        PageInfo<Epidemic> pageInfo = new PageInfo<>(list);
        Result result = new Result();
        if(list!=null){
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(pageInfo);
        }else{
            result.setMsg("查询失败");
            result.setCode(500);
        }
        return result;
    }

    //查看详情
    @GetMapping("/{id}")
    public Result findDetails(@PathVariable Integer id){
        System.out.println("查询细节的方法运行了");
        List<Epidemic> list = epidemicService.findDetails(id);
        Result result = new Result();
        if(list!=null){
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(list);
        }else{
            result.setMsg("查询失败");
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        System.out.println("删除的方法运行了");
        boolean flag = epidemicService.deleteById(id);
        Result result = new Result();
        if(flag){
            result.setMsg("撤销成功");
            result.setCode(200);
        }else{
            result.setMsg("撤销失败");
            result.setCode(500);
        }
        return result;
    }

    @RequestMapping("/check")
    public Result check(String uid){
        int state=epidemicService.check(uid);
        System.out.println(state+"状态码是这个");
        System.out.println(uid+"uid是这个");
        Result result = new Result();
        if(state==0){
            result.setCode(200);
        }else if(state==2){
            result.setMsg("尊敬的用户，您今天已经预约了");
            result.setCode(501);
        }else{
            result.setMsg("尊敬的用户，现在不是预约时间");
            result.setCode(502);
        }
        return result;
    }

    //查询核酸预约记录
    @RequestMapping("/findSumDataByPage")
    public Result findSumDataByPage(Integer currentPage,Integer pageSize){
        System.out.println("根据页数查询汇总数据方法运行了");
        List<EpidemicSum> list = epidemicService.findSumDataByPage(currentPage,pageSize);
        PageInfo<EpidemicSum> pageInfo = new PageInfo<>(list);
        Result result = new Result();
        if(list!=null){
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(pageInfo);
        }else{
            result.setMsg("查询失败");
            result.setCode(500);
        }
        return result;
    }

    @RequestMapping("/findPhone")
    public Result findPhone(){
        List<String> list = epidemicService.findPhone();
        Result result = new Result();
        if(list!=null){
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(list);
        }else{
            result.setMsg("查询失败");
            result.setCode(500);
        }
        return result;
    }

    @RequestMapping("/updateReservation")
    public Result updateReservation(Integer reservation){
       boolean flag = epidemicService.updateReservation(reservation);
        Result result = new Result();
        if(flag){
            result.setMsg("查询成功");
            result.setCode(200);
        }else{
            result.setMsg("查询失败");
            result.setCode(500);
        }
        return result;
    }
}
